package com.haoshuang.zhouzhoubang.ui.page.settled.upload

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityUploadBusinessLicenseBinding
import com.haoshuang.zhouzhoubang.model.event.RefreshTeamUserInfo
import com.haoshuang.zhouzhoubang.utils.MultiMediaChooseUtils
import com.haoshuang.zhouzhoubang.utils.OSSUploadUtils
import com.haoshuang.zhouzhoubang.widget.CustomUploadView
import com.haoshuang.zhouzhoubang.widget.UploadFileBean
import com.jeremyliao.liveeventbus.LiveEventBus
import com.jeremyliao.liveeventbus.core.LiveEvent
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.SpannableStringUtils
import com.mufeng.libs.utils.ktx.runOnUIThread
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.load


/**
 *  上传营业执照
 */
class UploadBusinessLicenseActivity :
    BaseVMActivity<UploadBusinessLicenseViewModel, ActivityUploadBusinessLicenseBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<UploadBusinessLicenseActivity>()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "营业执照"
        binding.titleBar.leftImageView().click { finish() }

        val tip = "资料已提交后， 系统将在1-3工作日进行审核， \n通过后门店推单接单恢复正常"
        val span = SpannableStringUtils(mActivity, tip)
            .first("推单接单")
            .textColor(R.color.c_ff0000)

        binding.tvTip.text = span

        binding.tvUpload.clickWithTrigger {
            val bean = binding.rivBusinessLicense.getSingleMedia()
            if (bean == null) {
                toast("请先上传图片")
                return@clickWithTrigger
            }
            vm.uploadStoreLicense(bean.url)
        }

    }

    override fun initData() {

    }

    override fun startObserve() {

        vm.uploadStoreLicenseData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("上传成功")
                LiveEventBus.get(RefreshTeamUserInfo::class.java).post(RefreshTeamUserInfo)
                finish()
            }
        }

    }

}